package com.biao.synchronized_test;

public class Test03_synchronized implements Runnable {
    private static int count = 100;

    @Override
    public void run() {
        while (true) {
            // 这里阻塞，模拟尽量让多个线程去抢call()执行
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            call();
        }
    }

    // synchronized加在实例方法上，使用的就是this锁
    private synchronized void call() {
        if (count > 1) {
            count--;
            System.out.println(Thread.currentThread().getName() + "---" + count);

        }
    }

    public static void main(String[] args) {
        Test03_synchronized test03Synchronized = new Test03_synchronized();
        // 让两个线程同时执行，去抢同一个资源变量
        new Thread(test03Synchronized).start();
        new Thread(test03Synchronized).start();

    }
}
